package com.c.ctgproject.adapter.guang

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.constraintlayout.utils.widget.ImageFilterView
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.entity.guang.CheckListCommonEntity
import com.c.ctgproject.ui.collect.check.CreatePassCheckActivity
import com.c.ctgproject.utils.MyConstants
import com.c.ctgproject.utils.noDataHandler
import org.jetbrains.anko.imageResource
import org.jetbrains.anko.startActivity

class MyCheckListAdapter(private val context: Context, private val mList: ArrayList<CheckListCommonEntity.DataBean.ListBean>, private val listener: MyCheckListAdapterListener) : BaseEmptyRecyclerViewAdapterV2(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_my_check_list_, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            holder.mNo.text = data.bizNo.noDataHandler()
            holder.carPlate.text = data.plate.noDataHandler()
            holder.unit.text = data.carryingEnterprise.noDataHandler()
            holder.mUserNameTV.text = data.createPersonName.noDataHandler()
            holder.mTimeTV.text = data.createTime.noDataHandler()

            //详情
            holder.mParentLL.setOnClickListener {
//                val show = data.status != 1
                context.startActivity<CheckDetailActivity>(
                    MyConstants.ID to data.stripId,
                    MyConstants.JUST_SHOW to true
                )

            }
            //放行条状态 0-新建(草稿) 1-待审批 2-已完成
            when (data.status) {
                0 -> {
                    holder.mBottomRL.visibility = View.VISIBLE
                    holder.mStatueIV.visibility = View.GONE
                    holder.mBTN.text = "继续编辑"
                }
                1 -> {
                    if (data.isEditable==1){
                        holder.mBottomRL.visibility = View.VISIBLE
                        holder.mStatueIV.visibility = View.GONE
                        holder.mBTN.text = "编辑"
                    }else{
                        //啥都没有
                        holder.mBottomRL.visibility = View.GONE
                        holder.mStatueIV.visibility = View.GONE
                    }
                }
                2 -> {
                    //已完成的展示通过标签
                    holder.mStatueIV.visibility = View.VISIBLE
                    if (data.isReject == 1) {
                        holder.mBTN.text = "修改申请"
                        holder.mStatueIV.imageResource = R.drawable.icon_refuse_right
                        //没通过的可以重新修改
                        holder.mBottomRL.visibility = View.VISIBLE
                    } else {
                        holder.mStatueIV.imageResource = R.drawable.icon_agree_right
                        holder.mBottomRL.visibility = View.GONE
                    }
                }
            }
            holder.mBTN.setOnClickListener {
                when (holder.mBTN.text) {
                    "编辑"->{
                        context.startActivity<CreatePassCheckActivity>(MyConstants.ID to data.stripId, MyConstants.UPDATE to true)
                    }

                    "继续编辑" -> {
                        context.startActivity<CreatePassCheckActivity>(MyConstants.ID to data.stripId, MyConstants.Draft to true)
                    }
                    "修改申请" -> {
                        context.startActivity<CreatePassCheckActivity>(MyConstants.ID to data.stripId,MyConstants.CHANGE to true)
                    }
                }
            }
            if (data.status == 0) {
                holder.mDeleteBTN.visibility = View.VISIBLE
            } else {
                holder.mDeleteBTN.visibility = View.GONE

            }
            //删除草稿
            holder.mDeleteBTN.setOnClickListener {
                listener.onItemDeleteClick(position)
            }
        }
    }

    interface MyCheckListAdapterListener {
        fun onItemDeleteClick(position: Int)
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val mStatueIV: ImageFilterView = view.findViewById(R.id.mStatueIV)
        val mNo: TextView = view.findViewById(R.id.mNo)
        val carPlate: TextView = view.findViewById(R.id.carPlate)
        val unit: TextView = view.findViewById(R.id.unit)
        val mTimeTV: TextView = view.findViewById(R.id.mTimeTV)
        val mUserNameTV: TextView = view.findViewById(R.id.mUserNameTV)
        val mBottomRL: RelativeLayout = view.findViewById(R.id.mBottomRL)
        val mBTN: Button = view.findViewById(R.id.mBTN)
        val mDeleteBTN: Button = view.findViewById(R.id.mDeleteBTN)
        val mParentLL: LinearLayoutCompat = view.findViewById(R.id.mParentLL)
    }

}